package com.wtwd.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.BloodOxygenRecordMapper;
import com.wtwd.device.model.entity.BloodOxygenInfo;
import com.wtwd.device.model.entity.BloodOxygenRecord;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.BloodOxygenRecordService;
import com.wtwd.device.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @date 2021/09/06
 * @description
 */
@Service
public class BloodOxygenRecordServiceImpl implements BloodOxygenRecordService {

    @Autowired
    private BloodOxygenRecordMapper bloodOxygenRecordMapper;


    @Override
    public RespEntity<List<BloodOxygenRecord>> getDaysBloodOxygenRecordList(Long deviceId, String startTime, String endTime) {
        List<BloodOxygenRecord> bloodOxygenRecords = bloodOxygenRecordMapper.getDaysBloodOxygenRecordList(deviceId, startTime, endTime);
        if (bloodOxygenRecords != null && bloodOxygenRecords.size() > 0) {
            bloodOxygenRecords.forEach(bloodOxygenRecord -> {
                bloodOxygenRecord.setListBloodOxygen(JSONArray.parseArray(bloodOxygenRecord.getBloodOxygenInfo(), BloodOxygenInfo.class));
            });
        }
        return new RespEntity(RespCode.SUCCESS, bloodOxygenRecords);
    }

    @Override
    public RespEntity<List<BloodOxygenRecord>> getMonthBloodOxygenRecordList(Long deviceId, String month) {
        List<BloodOxygenRecord> bloodOxygenRecords = bloodOxygenRecordMapper.getMonthBloodOxygenRecordList(deviceId, month);
        if (bloodOxygenRecords != null && bloodOxygenRecords.size() > 0) {
            bloodOxygenRecords.forEach(bloodOxygenRecord -> {
                bloodOxygenRecord.setListBloodOxygen(JSONArray.parseArray(bloodOxygenRecord.getBloodOxygenInfo(), BloodOxygenInfo.class));
            });
        }
        return new RespEntity(RespCode.SUCCESS, bloodOxygenRecords);
    }

    @Override
    public RespEntity addBloodOxygenRecord(BloodOxygenRecord bloodOxygenRecord) {
        String time = DateUtils.format(bloodOxygenRecord.getTime(), DateUtils.dateFormat5);
        BloodOxygenRecord bloodOxygenRecordOld = bloodOxygenRecordMapper.getBloodOxygenRecordByDeviceIdAndTime(bloodOxygenRecord.getDeviceId(),time);
        if (bloodOxygenRecordOld!=null){
            return new RespEntity(RespCode.TODAYS_DATA_ALREADY_EXIST);
        }
        bloodOxygenRecord.setBloodOxygenInfo(JSONArray.toJSONString(bloodOxygenRecord.getListBloodOxygen()));
        bloodOxygenRecord.setUploadTime(new Date().getTime());
        Integer rest = bloodOxygenRecordMapper.addBloodOxygenRecord(bloodOxygenRecord);
        return new RespEntity(rest>0?RespCode.SUCCESS:RespCode.FAIL);
    }

    @Override
    public Integer clearBloodOxygenRecordByDate(String date) {
        return bloodOxygenRecordMapper.clearBloodOxygenRecordByDate(date);
    }

    @Override
    public BloodOxygenRecord getBloodOxygenRecordByDeviceIdAndTime(Long id, String time) {
        return bloodOxygenRecordMapper.getBloodOxygenRecordByDeviceIdAndTime(id,time);
    }

    @Override
    public Integer deleteBloodOxygenRecordByDeviceId(Long id) {
        return bloodOxygenRecordMapper.deleteBloodOxygenRecordByDeviceId(id);
    }
}
